#include "signalgenerator.h"

extern char uartReBuff[REC_MAC_LENGTH];

SINGNAL_TypeDef mySignal;


void mySignalInit(void){
	mySignal.firstTrigFlag = TRIGGERNONE;
	mySignal.signal = mySignal.signalType.sine;
	mySignal.rectangleOn = SIGNALOFF;
	mySignal.sineOn = SIGNALOFF;
	mySignal.triangleOn = SIGNALOFF;
}

void StartDACSine(void)
{
	if (mySignal.firstTrigFlag == TRIGGERNONE)
	{
		//HAL_DAC_Start(&hdac, DAC_CHANNEL_1);
		if (DACOutConfig() == HAL_OK)
		{
			printf("\r\n*******Key in Sine again to stop!*******\r\n\r\n");
			char myArray[4] = {'1','2','0','9'};
			// uint16_t i = atoi(myArray);
			// printf("%d\r\n", i);
			mySignal.firstTrigFlag = TRIGGED;
			mySignal.enterParameterFlag = CHANGEPARAON;
		}
		else
		{
			Error_Handler();
		}
	}
	else
	{
		HAL_DAC_Stop(&hdac, DAC_CHANNEL_1);
		mySignal.firstTrigFlag = TRIGGERNONE;
	}
}

HAL_StatusTypeDef TIMParamDef(uint32_t prescaler, uint32_t period, TIM_HandleTypeDef *tim)
{
	if (tim == &htim6)
	{
		if (prescaler >= 1 && prescaler <= 199 && period >= 1 && period <= 1999)
		{
			if(HAL_TIM_Base_Stop(tim) != HAL_OK){
				Error_Handler();
			}
			tim->Init.Prescaler = prescaler;
			tim->Init.Period = period;
			if (HAL_TIM_Base_Init(tim) != HAL_OK)
			{
				Error_Handler();
			}
			if(HAL_TIM_Base_Start(tim) != HAL_OK){
				Error_Handler();
			}
			return HAL_OK;
		}
		else
		{
			printf("Parameter error! \r\n");
			return HAL_ERROR;
		}
	}
	else
	{
		printf("TIM Select error! \r\n");
		return HAL_ERROR;
	}
}

void singnalCal(char *uartBuff){
	uint8_t temCnt = 0;
	while(uartBuff[temCnt] != '-'){
		temCnt++;
		if(temCnt > 10){
			printf("Input Error A!\r\n");
			return ;
		}
	}
	char writeDataL[temCnt];// 每一个'-'前的数字
	for(uint8_t i = 0; i < temCnt; i++){
		writeDataL[i] = uartBuff[i];
	}
	temCnt++;
	uint8_t temValue = temCnt;
	while(uartBuff[temCnt] != '-'){
		temCnt++;
		if(temCnt - temValue > 10){
			printf("Input Error B!\r\n");
			return ;
		}
	}
	char writeDataH[temCnt - temValue];
	for(uint8_t j = 0; j < temCnt - temValue; j++){
		writeDataH[j] = uartBuff[temValue + j];
	}
	uint16_t paraA = atoi(writeDataL);
	uint16_t paraB = atoi(writeDataH);
	if(TIMParamDef(paraA, paraB, &htim6) != HAL_OK){
		Error_Handler();
	}
}

